from template import *
from collect import collect


import sys

itime = -1
if len(sys.argv) == 2:
	t = int(sys.argv[1])
elif len(sys.argv) > 2:
	print("error: should have one argument, as time step")
else:
	t = itime



amplification_factor = 100.0

nx = 6001

dx = 0.5e-5  # unit (m)
x = np.linspace(0, nx * dx, nx)
x = x * amplification_factor

xmin = x.min()
xmax = x.max()

list_potential = []
list_n = []
list_nC = []
list_nC1 = []
list_Te = []
list_TD1 = []
list_qe = []
list_qD1 = []

list_label = ["Rc = 0.02", "Rc = 0.04", "Rc = 0.06", "Rc = 0.08", "Rc = 0.1"]

list_path = ["conduction_limit_C0.02/data", "conduction_limit_C0.04/data", "conduction_limit_C0.06/data", "conduction_limit_C0.08/data", "conduction_limit_C0.1/data"]
for path_item in list_path:
    val = collect("/Fields/", "Phi_global_avg", path = path_item)[t, 0, 0, :]
    list_potential.append(val)

    val = collect("/Fields/", "Rho_global_e_avg", path = path_item)[t, 0, 0, :]
    list_n.append(val)

    val = collect("/Fields/", "Rho_global_C_avg", path = path_item)[t, 0, 0, :]
    list_nC.append(val)

    val = collect("/Fields/", "Rho_global_C1_avg", path = path_item)[t, 0, 0, :]
    list_nC1.append(val)

    val = collect("/Fields/", "T_global_e_avg", path = path_item)[t, 0, 0, :]
    list_Te.append(val)

    val = collect("/Fields/", "T_global_D1_avg", path = path_item)[t, 0, 0, :]
    list_TD1.append(val)

    val = collect("/Fields/", "q_global_e_avg", path = path_item)[t, 0, 0, :]
    list_qe.append(val)
    
    val = collect("/Fields/", "q_global_D1_avg", path = path_item)[t, 0, 0, :]
    list_qD1.append(val)



##inite the fig of matplotlib
fig=plt.figure(figsize=(15,10))
fig.subplots_adjust(top=0.9,bottom=0.1,wspace=0.3,hspace=0.35)

##============potential======================================================
ax=fig.add_subplot(4,2,1)

for i in range(len(list_potential)):
    line0=ax.plot(x, list_potential[i], label = list_label[i])

ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$\phi \mathrm{(V)}$", fontsize = label_fontsize)
#ax.grid(True)

num1 = 0
num2 = 1.4
num3 = 2
num4 = 0
handles, labels = ax.get_legend_handles_labels()
ax.legend(flip(handles, 3), flip(labels, 3), framealpha = 0.2, bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4, ncol = 3)


ax.annotate("(a)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)

##============density e======================================================
ax=fig.add_subplot(4,2,2)

for i in range(len(list_n)):
    line0=ax.plot(x, list_n[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$n \mathrm{(m^{-3})}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(b)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)

##============density C======================================================
ax=fig.add_subplot(4,2,3)

for i in range(len(list_n)):
    line0=ax.plot(x, list_nC[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$n \mathrm{(m^{-3})}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(b)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)

##============density C1======================================================
ax=fig.add_subplot(4,2,4)

for i in range(len(list_n)):
    line0=ax.plot(x, list_nC1[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$n \mathrm{(m^{-3})}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(b)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)

##============ Te ======================================================
ax=fig.add_subplot(4,2,5)

for i in range(len(list_Te)):
    line0=ax.plot(x, list_Te[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$T \mathrm{_e (eV)}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(c)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)



##============TD1======================================================
ax=fig.add_subplot(4,2,6)

for i in range(len(list_TD1)):
    line0=ax.plot(x, list_TD1[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$T \mathrm{_{D^{+}} (eV)}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(d)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)


##============qe======================================================
ax=fig.add_subplot(4,2,7)

for i in range(len(list_qe)):
    line0=ax.plot(x, list_qe[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
#ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$q \mathrm{_e (Wm^{-2})}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(e)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)


##============qD1======================================================
ax=fig.add_subplot(4,2,8)

for i in range(len(list_qD1)):
    line0=ax.plot(x, list_qD1[i], label = list_label[i])

ax.set_xlim([xmin, xmax])
#ax.set_ylim(bottom = 0.0)
ax.set_ylabel(r"$q \mathrm{_{D^{+}} (Wm^{-2})}$", fontsize = label_fontsize)
#ax.grid(True)
#ax.legend(framealpha = 0.2)
ax.annotate("(f)", xy=get_axis_limits(ax, x_scale = -0.1, y_scale = -0.15), annotation_clip=False)


fig_file_name = "Profiles_all_rC_" + str(t) + ".svg"
fig.savefig(fig_file_name, dpi = 300)
##fig.show()       #when the program finishes,the figure disappears
#plt.axis('equal')
#plt.show()         #The command is OK
